//
//  fstyle.swift
//  xzseller
//
//  Created by zhuxietong on 2021/2/2.
//

import Foundation
import UIKit
//public enum UICss {
//    case font(UIFont)
//    case color(UIColor)
//
//    public var key:String{
//        get{
//            switch self {
//            case .font:
//                return "font"
//            default:
//                return "color"
//            }
//        }
//    }
//}

enum CSSArrangement{
    
}


public enum UICssDirection : Int {

    case horizontal = 0

    case vertical = 1
}
public enum UICss {
    case color(UIColor)
    case font(UIFont)
    case size(CGSize)
    case height(CGFloat)
    case width(CGFloat)
    case offsetY(CGFloat)
    case backgroundColor(UIColor)
    case cornerRadius(CGFloat)
    case space(CGFloat)
    case padding(UIEdgeInsets)
    case border(width:CGFloat,color:UIColor,radius:CGFloat)
    case direction(UICssDirection)
    
    
    public var priority:CGFloat{
        switch self {
        case .size:
            return 800
        default:
            return 1000
        }
    }
    
    public var key:String{
        switch self {
        case .direction:
            return "direction"
        case .height:
            return "height"
        case .offsetY:
            return "offsetY"
        case .size:
            return "size"
        case .backgroundColor:
            return "backgroundColor"
        case .cornerRadius:
            return "cornerRadius"
        case .space(_):
            return "space"
        case .padding(_):
            return "padding"
        case .border:
            return "border"
        case .font:
            return "font"
        case .width:
            return "width"
        case .color:
            return "color"
        }
      
    }
    
}


extension Array where Element == UICss{
    
    public func index(for element:Element) -> Int? {
        for (i,item) in self.enumerated() {
            if item.key == element.key{
                return i
            }
        }
        return nil
    }

    @inlinable public mutating func update(_ newElement: Element){
        if let index = index(for: newElement){
            self.insert(newElement, at: index)
            self.remove(at: index + 1)
        }else{
            self.append(newElement)
        }
    }
    
    
    public var padding:UIEdgeInsets{
        for p in self {
            switch p {
            case .padding(let padding):
                return padding
            default:
                break
            }
        }
        return .zero
    }
    public var width:CGFloat{
        let ks = self.sorted{$0.priority > $1.priority}
        for item in ks {
            switch item {
            case .width(let w):
                return w
            case .size(let size):
                return size.width
            default:
                break
            }
        }
        return 0
    }
    public var height:CGFloat{
        let ks = self.sorted{$0.priority > $1.priority}
        for item in ks {
            switch item {
            case .height(let h):
                return h
            case .size(let size):
                return size.height
            default:
                break
            }
        }
        return 0
    }
    public var size:CGSize{
        for item in self {
            switch item {
            case .size(let size):
                return size
            default:
                break
            }
        }
        return CGSize(width: 300, height: 300)
    }
    
    public var offsetY:CGFloat{
        for item in self {
        
            switch item {
            case .offsetY(let y):
                return y
            default:
                break
            }
        }
        return 0
    }
    public var backgroundColor:UIColor{
        for item in self {
            switch item {
            case .backgroundColor(let color):
                return color
            default:
                break
            }
        }
        return ._background
    }
    public var cornerRadius:CGFloat{
        for item in self {
            switch item {
            case .cornerRadius(let v):
                return v
            default:
                break
            }
        }
        return 0
    }
    public var color:UIColor{
        for p in self {
            switch p {
            case .color(let color):
                return color
            default:
                break
            }
        }
        return ._foreground
    }
    public var space:CGFloat{
        for p in self {
            switch p {
            case .space(let space):
                return space
            default:
                break
            }
        }
        return 0
    }
    public var font:UIFont{
        for p in self {
            switch p {
            case .font(let font):
                return font
            default:
                break
            }
        }
        return .systemFont(ofSize: 14)
    }
    
    public mutating func set(_ style:UICss){
        var index:Int? = nil
        for (i,p) in self.enumerated() {
            if p.key == style.key{
                index = i
            }
        }
        if let i = index{
            remove(at: i)
        }
        self.insert(style, at: 0)        
    }
    
}
